import { baseApiUrl, path } from "../../utils/config";
import { course } from '../../utils/api';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,

    type: 1,
    show: false,
    type_list: [],
    type_id: "",

    page: 1,
    last_page: 1,
    list: []
  },
  async onLoad(options) {
    let { type } = options;
    let { page } = this.data;
    let res = await course({
      is_free: type,
      class_id: this.type_id,
      page,
      limit: 10
    });
    let type_list = res.class;
    res = res.course;
    let { data, last_page } = res;
    let list = page == 1 ? data : [...this.data.list, ...data];
    this.setData({ list, last_page, type_list, type });
  },
  onReachBottom() {
    let page = this.data.page;
    let last_page = this.data.last_page;
    if (page == last_page) return false;
    page += 1;
    this.setData({ page });
    this.getList();
  },
  async getList() {
    let { type } = this.data;
    let res = await course({
      is_free: type,
      class_id: this.data.type_id,
      page: this.data.page,
      limit: 10
    });
    res = res.course;
    let { data, last_page } = res;
    let list = this.data.page == 1 ? data : [...this.data.list, ...data];
    this.setData({ list, last_page })
  },
  changeDrop() {
    this.setData({ show: !this.data.show })
  },
  typeChange(e) {
    const item = e.currentTarget.dataset.item;
    let type_id = item.id;
    this.setData({ type_id })
  },
  submit() {
    this.setData({ show: false, list: [], page: 1 });
    this.getList();
  },
  router(e) {
    let { url } = e.currentTarget.dataset;
    wx.navigateTo({ url });
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
});